package com.qf.xvyinghai02.controller;

import com.qf.xvyinghai02.entity.Order;
import com.qf.xvyinghai02.entity.Product;
import com.qf.xvyinghai02.service.OrderService;
import com.qf.xvyinghai02.service.ProductService;
import com.qf.xvyinghai02.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping
public class BarController {
    @Autowired
    private ProductService productService;
    @Autowired
    private OrderService orderService;

    @RequestMapping("/bar")
    public ResultVo bar() {
        List<Product> productList = productService.list();
        List<Order> orderList = orderService.list();
        Map<Integer, String> productMap = new HashMap<>();
        for (Product product : productList) {
            productMap.put(product.getPid(), product.getCategories());
        }
        Map<String, List<String>> map = new HashMap<>();
        List<String> categories = new ArrayList<>();
        List<String> values = new ArrayList<>();
        for (Order order : orderList) {
            Integer salesvolume = order.getSalesvolume();
            Integer pid = order.getPid();
            categories.add(productMap.get(pid));
            values.add(salesvolume.toString());
        }
        map.put("categories", categories);
        map.put("values", values);
        return ResultVo.success("ok", map);
    }
}
